package tutorial.lite_mod.account_book;


import com.example.SpecialApplicationAfter4Example;
import com.example.SpecialApplicationBefore4Example;
import com.wholefarm.model.SpecailApplicationInterface;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.log4j.Logger;
import public_util.database.ConnectionFactory;
import public_util.model.*;
import public_util.pack_4_stable.tools.DateTool;
import public_util.pack_4_stable.tools.EnCodeTool;
import public_util.servlet.ServletParent;
import public_util.tool_4_handle.Tool_4_Servlet;
import system_api_4_trade.DuzikaiSpecialApplicationFactory;
import system_api_4_trade.SpecialApplicationAfter;
import system_api_4_trade.SpecialApplicationBefore;
import system_api_4_trade.dao.ApiPublicGoodsOrderDetailMapper;
import system_api_4_trade.dao.ApiPublicMiddleTableMapper;
import system_api_4_trade.dao.ApiPublicUserInfoMapper;
import system_api_4_trade.util.sql.InsertTool;
import system_api_4_trade.util.sql.SelectTool;
import system_api_4_trade.util.tool_4_handle.DateToolApi;
import system_api_4_trade.util.tool_4_handle.StringToolApi;
import system_api_4_trade.util.tool_4_handle.Tool_4_ServletApi;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


class Tutorial4LiteModTool {

    private static Logger logger = Logger.getLogger(Tutorial4LiteModTool.class);




    public static ReturnData show_stat_4_income_outcome(SqlSessionFactory sqlSession, ServletParent thisParent, HttpSession hsession, HttpServletResponse response, HttpServletRequest request){

        logger.info("----show_stat----");
        ReturnData resultData = new ReturnData();
        PublicDataAdminUser adminUser = (PublicDataAdminUser) Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_data_admin_user);
        PublicAgentInfo agentInfo = (PublicAgentInfo)Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_agent_info);
        resultData = Tool_4_ServletApi.checkAccessType(hsession, resultData, 102);
        if (resultData.getStatus() != 0)
        {
            return resultData;
        }

        String id_admin = "-1";

        if (agentInfo != null)
        {
            id_admin = agentInfo.getId_public_data_admin_user() + "";
        }

        if (adminUser != null)
        {
            id_admin = adminUser.getId_public_data_admin_user() + "";
        }

        String begin_time = request.getParameter(Const4PublicDataBase.PARA_begin_time);
        String end_time = request.getParameter(Const4PublicDataBase.PARA_end_time);
        if (begin_time == null)
        {
            resultData.setMsg("begin_time未传入");
            return resultData;
        }
        if (end_time == null)
        {
            resultData.setMsg("end_time未传入");
            return resultData;
        }
        String id_public_user_info = request.getParameter(Const4PublicDataBase.PARA_id_public_user_info);
        id_public_user_info = StringToolApi.paraHttpFilter(id_public_user_info);
        if (id_public_user_info == null)
        {
            resultData.setMsg("用户ID未传入");
            return resultData;
        }
        SqlSession session = sqlSession.openSession();
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Transaction newTransaction = transactionFactory.newTransaction(session.getConnection(), false);

        Tutorial4LiteModMapper tlMopper = session.getMapper(Tutorial4LiteModMapper.class);//小模式
        try {


            HashMap<String, Object> para4inOut = new HashMap<String, Object>();
            para4inOut.put(Const4PublicDataBase.PARA_id_public_user_info, id_public_user_info);
            para4inOut.put(Const4PublicDataBase.PARA_is_online, 0);
            para4inOut.put(Const4PublicDataBase.PARA_begin_time, begin_time);
            para4inOut.put(Const4PublicDataBase.PARA_end_time, end_time);
            List<PublicMiddleTable> result_all = tlMopper.show_stat_4_income_outcome(para4inOut);


            HashMap<String, Object> returnData = new HashMap<String, Object>();
            returnData.put("income_outcome", result_all);
            resultData.setStatus(0);
            resultData.setReturnData(returnData);
            return resultData;
        }
        catch(Exception e)
        {
            try {
                newTransaction.rollback();
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;

            } catch (SQLException e1) {
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;
            }
        }
        finally
        {
            session.close();
        }
    }





    public static ReturnData show_stat_4_class(SqlSessionFactory sqlSession, ServletParent thisParent, HttpSession hsession, HttpServletResponse response, HttpServletRequest request){

        logger.info("----show_stat_4_class----");
        ReturnData resultData = new ReturnData();
        PublicDataAdminUser adminUser = (PublicDataAdminUser) Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_data_admin_user);
        PublicAgentInfo agentInfo = (PublicAgentInfo)Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_agent_info);
        resultData = Tool_4_ServletApi.checkAccessType(hsession, resultData, 102);
        if (resultData.getStatus() != 0)
        {
            return resultData;
        }

        String id_admin = "-1";

        if (agentInfo != null)
        {
            id_admin = agentInfo.getId_public_data_admin_user() + "";
        }

        if (adminUser != null)
        {
            id_admin = adminUser.getId_public_data_admin_user() + "";
        }

        String begin_time = request.getParameter(Const4PublicDataBase.PARA_begin_time);
        String end_time = request.getParameter(Const4PublicDataBase.PARA_end_time);
        if (begin_time == null)
        {
            resultData.setMsg("begin_time未传入");
            return resultData;
        }
        if (end_time == null)
        {
            resultData.setMsg("end_time未传入");
            return resultData;
        }
        String id_public_user_info = request.getParameter(Const4PublicDataBase.PARA_id_public_user_info);
        id_public_user_info = StringToolApi.paraHttpFilter(id_public_user_info);

        if (id_public_user_info == null)
        {
            resultData.setMsg("用户ID未传入");
            return resultData;
        }


        String mid_table_remark_001 = request.getParameter(Const4PublicDataBase.PARA_mid_table_remark_001);
        if (mid_table_remark_001 == null)
        {
            resultData.setMsg("mid_table_remark_001未传入");
            return resultData;
        }


        SqlSession session = sqlSession.openSession();
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Transaction newTransaction = transactionFactory.newTransaction(session.getConnection(), false);

        Tutorial4LiteModMapper tlMopper = session.getMapper(Tutorial4LiteModMapper.class);//小模式
        try {


            HashMap<String, Object> para4inOut = new HashMap<String, Object>();
            para4inOut.put(Const4PublicDataBase.PARA_id_public_user_info, id_public_user_info);
            para4inOut.put(Const4PublicDataBase.PARA_is_online, 0);
            para4inOut.put(Const4PublicDataBase.PARA_mid_table_remark_001, mid_table_remark_001);
            para4inOut.put(Const4PublicDataBase.PARA_begin_time, begin_time);
            para4inOut.put(Const4PublicDataBase.PARA_end_time, end_time);
            List<PublicMiddleTable> result_all = tlMopper.show_stat_4_class(para4inOut);


            HashMap<String, Object> returnData = new HashMap<String, Object>();
            returnData.put("class_data", result_all);
            resultData.setStatus(0);
            resultData.setReturnData(returnData);
            return resultData;
        }
        catch(Exception e)
        {
            try {
                newTransaction.rollback();
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;

            } catch (SQLException e1) {
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;
            }
        }
        finally
        {
            session.close();
        }
    }







    public static ReturnData show_stat_4_list(SqlSessionFactory sqlSession, ServletParent thisParent, HttpSession hsession, HttpServletResponse response, HttpServletRequest request){

        logger.info("----show_stat_4_list----");
        ReturnData resultData = new ReturnData();
        PublicDataAdminUser adminUser = (PublicDataAdminUser) Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_data_admin_user);
        PublicAgentInfo agentInfo = (PublicAgentInfo)Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_agent_info);
        resultData = Tool_4_ServletApi.checkAccessType(hsession, resultData, 102);
        if (resultData.getStatus() != 0)
        {
            return resultData;
        }

        String id_admin = "-1";

        if (agentInfo != null)
        {
            id_admin = agentInfo.getId_public_data_admin_user() + "";
        }

        if (adminUser != null)
        {
            id_admin = adminUser.getId_public_data_admin_user() + "";
        }

        String begin_time = request.getParameter(Const4PublicDataBase.PARA_begin_time);
        String end_time = request.getParameter(Const4PublicDataBase.PARA_end_time);
        if (begin_time == null)
        {
            resultData.setMsg("begin_time未传入");
            return resultData;
        }

        String begin_time_final = DateToolApi.format(DateToolApi.parseDate("yyyyMMdd", begin_time), "yyyy-MM-dd");
        String end_time_final = DateToolApi.format(DateToolApi.parseDate("yyyyMMdd", end_time), "yyyy-MM-dd");
        if (end_time == null)
        {
            resultData.setMsg("end_time未传入");
            return resultData;
        }
        String id_public_user_info = request.getParameter(Const4PublicDataBase.PARA_id_public_user_info);
        id_public_user_info = StringToolApi.paraHttpFilter(id_public_user_info);

        if (id_public_user_info == null)
        {
            resultData.setMsg("用户ID未传入");
            return resultData;
        }


        String mid_table_remark_001 = request.getParameter(Const4PublicDataBase.PARA_mid_table_remark_001);
        if (mid_table_remark_001 == null)
        {
            resultData.setMsg("mid_table_remark_001未传入");
            return resultData;
        }
        String date_list_type = request.getParameter("date_list_type");
        if (date_list_type == null)
        {
            resultData.setMsg("date_list_type未传入");
            return resultData;
        }



        SqlSession session = sqlSession.openSession();
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Transaction newTransaction = transactionFactory.newTransaction(session.getConnection(), false);

        Tutorial4LiteModMapper tlMopper = session.getMapper(Tutorial4LiteModMapper.class);//小模式
        try {


            List<PublicMiddleTable> result_all = null;

            if ("day".equals(date_list_type))
            {
                HashMap<String, Object> para4inOut = new HashMap<String, Object>();
                para4inOut.put(Const4PublicDataBase.PARA_id_public_user_info, id_public_user_info);
                para4inOut.put(Const4PublicDataBase.PARA_is_online, 0);
                para4inOut.put(Const4PublicDataBase.PARA_mid_table_remark_001, mid_table_remark_001);
                para4inOut.put(Const4PublicDataBase.PARA_begin_time, begin_time_final);
                para4inOut.put(Const4PublicDataBase.PARA_end_time, end_time_final);
                result_all = tlMopper.show_stat_4_list_day(para4inOut);
            }


            if ("month".equals(date_list_type))
            {

                end_time_final = DateToolApi.format(DateToolApi.parseDate("yyyyMMdd", end_time), "yyyyMM");
                HashMap<String, Object> para4inOut = new HashMap<String, Object>();
                para4inOut.put(Const4PublicDataBase.PARA_id_public_user_info, id_public_user_info);
                para4inOut.put(Const4PublicDataBase.PARA_is_online, 0);
                para4inOut.put(Const4PublicDataBase.PARA_mid_table_remark_001, mid_table_remark_001);
                para4inOut.put(Const4PublicDataBase.PARA_begin_time, begin_time_final);
                para4inOut.put(Const4PublicDataBase.PARA_end_time, end_time_final);
                result_all = tlMopper.show_stat_4_list_month(para4inOut);
            }


            if ("year".equals(date_list_type))
            {
                end_time_final = DateToolApi.format(DateToolApi.parseDate("yyyyMMdd", end_time), "yyyy");
                HashMap<String, Object> para4inOut = new HashMap<String, Object>();
                para4inOut.put(Const4PublicDataBase.PARA_id_public_user_info, id_public_user_info);
                para4inOut.put(Const4PublicDataBase.PARA_is_online, 0);
                para4inOut.put(Const4PublicDataBase.PARA_mid_table_remark_001, mid_table_remark_001);
                para4inOut.put(Const4PublicDataBase.PARA_begin_time, begin_time);
                para4inOut.put(Const4PublicDataBase.PARA_end_time, end_time);
                result_all = tlMopper.show_stat_4_list_year(para4inOut);
            }

            HashMap<String, Object> returnData = new HashMap<String, Object>();
            returnData.put("class_data", result_all);
            resultData.setStatus(0);
            resultData.setReturnData(returnData);
            return resultData;
        }
        catch(Exception e)
        {
            try {
                newTransaction.rollback();
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;

            } catch (SQLException e1) {
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;
            }
        }
        finally
        {
            session.close();
        }
    }






    public static void main(String[] args) throws Exception {

        /**
         * 验证supply的批量更新, 后面像加入购物车的操作,就会变得比较简单
         */
        SpecialApplicationBefore beforeApp = new SpecialApplicationBefore4Example();
        //本地环境
        beforeApp.setConfiguartion_environment(new String[]{SpecailApplicationInterface.LOAD_CONF_TYPE_FROM_DB, "1021"});
        /*按照独自开重构思路, 进行相关的特殊处理的方法*/
        SpecialApplicationAfter afterApp = new SpecialApplicationAfter4Example();
        DuzikaiSpecialApplicationFactory.setInitApplication(beforeApp, afterApp);
        SqlSessionFactory sqlSession = ConnectionFactory.getSession();
        SqlSession session = sqlSession.openSession();
        try
        {
            session.commit();
        }
        catch(Exception e)
        {
            Tool_4_Servlet.showStackTrace(e);
            logger.info(e);
        }
        finally
        {
            session.close();
        }
    }

}